//
//  VisitorLoginView.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/6/24.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate:NSObjectProtocol{

    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()

}

class VisitorLoginView: UIView {
    
    
    //设置代理
    weak var delegate:VisitorLoginViewDelegate?
    
    
    override init(frame:CGRect){
    
        super.init(frame:frame)
    
        setUpUI()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpViewInfo(iconName:String , message:String ,isHome:Bool){
        messageLabel.text = message
        
        //判断是否是首页
        if isHome{
            homeIconImage.hidden = false
            iconImage.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        
        }else{
        
            homeIconImage.hidden = true
            iconImage.image = UIImage(named: iconName)
            
            sendSubviewToBack(maskIconImage)
        
        }
    
    
    }
    
    func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 10
        
        anim.repeatCount = MAXFLOAT
        
        iconImage.layer.addAnimation(anim, forKey: nil)
    
    
    }
    
    
    //创建UI
    
    private func setUpUI(){
    
        addSubview(iconImage)
        addSubview(maskIconImage)
        addSubview(homeIconImage)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //MARK:自动布局
        
        //iconImage
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //maskIconImage
        maskIconImage.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-140-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconImage]))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        
        //homeIconImage
        homeIconImage.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: homeIconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //messageLabel
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 40))
        
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":messageLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":messageLabel]))
       
        
        //registerButton
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 40))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        
        //loginButton
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 40))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        
        
        
    
    }
    
    func register(){
        print(__FUNCTION__)
        delegate?.visitorRegisterButtonClicked()
    
    }
    
    
    func login(){
        print(__FUNCTION__)
        delegate?.visitorLoginButtonClicked()
    
    }
    
    
    ///MARK:懒加载控件
    //图标
    lazy var iconImage: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return UIImageView(image: image)
   
    }()

    //遮罩
    lazy var maskIconImage: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    
    }()
    
    
    //房子
    lazy var homeIconImage: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
        
    }()
    
    
    //文字标签
    lazy var messageLabel: UILabel = {
    
        let message = UILabel(frame: CGRectMake(0, 0, 240, 40))
        
        message.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        message.tintColor = UIColor.lightGrayColor()
        message.font = UIFont.systemFontOfSize(14)
        message.textAlignment = NSTextAlignment.Center
        message.preferredMaxLayoutWidth = 224
        
        message.numberOfLines = 2
        
        message.sizeToFit()
        
        return message
    
    }()
    
    
    //注册按钮
    lazy var registerButton: UIButton = {
    
        let register = UIButton(frame: CGRectMake(0, 0, 100, 40))
        
        register.setTitle("注册", forState: UIControlState.Normal)
        
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        register.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
    }()
    
    //登录按钮
    lazy var loginButton: UIButton = {
    
        let login = UIButton(frame: CGRectMake(200, 200, 100, 40))
        
        login.setTitle("登录", forState: UIControlState.Normal)
        
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        login.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
        
    
    }()
    
    
   

}
